package com.unionPay.utils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.unionPay.net.HttpTask;







import android.app.Activity;



public class Utils {
	
	public static boolean validateMoblie(String phone) {
		int l = phone.length();
		boolean rs = false;
		switch (l) {
		case 7:
			if (matchingText("^(13[0-9]|15[0-9]|18[0-9]|14[5|7])\\d{4}$", phone)) {
				rs = true;
			}
			break;
		case 11:
			if (matchingText("^(13[0-9]|15[0-9]|18[0-9]|14[5|7])\\d{4,8}$", phone)) {
				rs = true;
			}
			break;
		default:
			rs = false;
			break;
		}
		return rs;
	}

	private static boolean matchingText(String expression, String text) {
		Pattern p = Pattern.compile(expression); // ������ʽ
		Matcher m = p.matcher(text); // �������ַ�
		boolean b = m.matches();
		return b;
	}
	
	public static boolean isValidUserName(String name){
		if(name == null){
			return false;
		}
		String check_china = "[\u4E00-\u9FA5]{2,5}(?:��[\u4E00-\u9FA5]{2,5})*";
		Pattern regex = Pattern.compile(check_china);
		Matcher mather = regex.matcher(name);		
		return mather.matches();
	}
	
	public static String amtFormat(String src) {
		if (src != null && !src.equals("")) {
			DecimalFormat format = new DecimalFormat("#0.00");
			return format.format(Double.parseDouble(src) / 100);
		}
		return null;
	}
	
	public static HttpTask getHttpTask(String url, String content) {
		
		HttpTask httpTask = null;
		httpTask = new HttpTask(null, url, content);
		return httpTask;

	}
	
	public static Activity activity;
	
	/** �Ա��ļ��� */
	public static byte[] DES3key;

	public static String encode(String mechantId,String Message) throws Exception {
		String sendMessage = "";
		// ��ʼ����Կ
		GlobalInfo.myprivateKye = getMyprivateKey();
		DES3key = GlobalInfo.myprivateKye.getBytes();
		sendMessage = Base64Encode(mechantId) + "|"
				+ RsaEncode(DES3key, GlobalInfo.frontPubKey.getBytes())
				+ "|" +Des3Encode(Message, DES3key);
		sendMessage = sendMessage.replace("\r\n", "");
		sendMessage = sendMessage.replace("\r", "");
		sendMessage = sendMessage.replace("\n", "");
		return sendMessage;
	}
	
	public static String getMyprivateKey() {
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < 24; i++) {
			sb.append(Math.abs(random.nextInt()) % 10);
		}
		return sb.toString();
	}

	public static String Base64Encode(String str) {
		return Base64Class.encodeToString(str.getBytes(), 0);
	}

	public static String RsaEncode(byte[] des3KeyData, byte[] front) {
		// ����
		byte[] encodedData2 = null;
		try {
			encodedData2 = RSAcode.encryptByPublicKey(des3KeyData,
					Base64Class.decode(front, 0));
		} catch (Exception e) {
			e.printStackTrace();
		}
		String temp = Base64Class.encodeToString(encodedData2, 0);
		String tempstr = "";
		for (int i = 0; i < temp.toCharArray().length; i++) {
			if (temp.charAt(i) != '\r' && temp.charAt(i) != '\n') {
				tempstr += temp.charAt(i);
			}
		}
		return tempstr;
	}

	public static String Des3Encode(String str, byte[] des3KeyData) {
		byte[] inputData = null;
		inputData = str.getBytes();
		// ����inputStr
		try {
			inputData = DES3code.encrypt(inputData, des3KeyData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Base64Class.encodeToString(inputData, 0);
	}

	public static String Md5Encode(String str) {
		byte[] md5 = Md5Util.MD5(str);
		String base64str = Base64Class.encodeToString(md5, 0);
		return Utils.getString(base64str);
	}

	public static String getString(String str) {
		String tempstr = "";
		for (int i = 0; i < str.toCharArray().length; i++) {
			if (str.charAt(i) != '\r' && str.charAt(i) != '\n') {
				tempstr += str.charAt(i);
			}
		}
		return tempstr;
	}

	public static String outputStr;

	public static boolean decode(String str) {
		if (str == null) {
			return false;
		}
		
		String[] temp = str.split("\\|");
		if (temp.length != 3) {
			return false;
		}
		if ("0".equals(temp[0])) { // ����������ʧ��
			try {
				outputStr = new String(Base64Class.decode(temp[2], 0), "UTF-8");
				GlobalInfo.netResult = null;
				return false;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		} else if ("1".equals(temp[0])) { // �����������ɹ�
			try {
				String text1 = new String(Des3Decode(
						Base64Class.decode(temp[1], 0), DES3key), "UTF-8");

				GlobalInfo.netResult = text1;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}

	public static byte[] Des3Decode(byte[] str, byte[] des3KeyData) {
		byte[] inputData = null;
		try {
			inputData = DES3code.decrypt(str, des3KeyData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return inputData;
	}

	public static String getTime(String str) {
		if(str == null){
			return null;
		}
		if (str.length() != 14) {
			return str;
		}
		return str.substring(0, 4) + "-" + str.substring(4, 6) + "-"
				+ str.substring(6, 8) + " " + str.substring(8, 10) + ":"
				+ str.substring(10, 12) + ":" + str.substring(12, 14);
	}

	public static int getLog(String title, String messge) {
		// return Log.i(title + ": ", messge);
		return 0;

	}

	public static String getSendTime() {
		Date d = new Date();
		// String str = d.toString();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddkkmmss");
		String str1 = sdf.format(d);
		return str1;
	}
	
	
	
	public static String toMAuthQueryXml(String merchantId,
			String merchantUserId){
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
				+ "<BindPay application=\"MAuthQuery.Req\" version=\"001-1.0.0\" sendTime=\""
				+ Utils.getSendTime()
				+ "\" sendSeqId=\"" + "1.0.0" + "\">"
				+ "<merchantId>" + merchantId + "</merchantId>"
				+ "<merchantUserId>"+ merchantUserId+ "</merchantUserId>"
				+ "<msgExt>" + "" +"</msgExt>"
				+ "<misc>" + "" + "</misc>"
				+ "</BindPay>";
		return content;
	}
	
	
	public static String toMGwXml(String merchantId,String merchantOrderId,
			String merchantOrderAmt,String merchantUserId){
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
				+ "<BindPay application=\"MGw.Req\" version=\"001-1.0.0\" sendTime=\""
				+ Utils.getSendTime()
				+ "\" sendSeqId=\"" + "1.0.0" + "\">"
				+ "<merchantId>" + merchantId + "</merchantId>"
				+ "<merchantOrderId>"+ merchantOrderId+ "</merchantOrderId>"
				+ "<merchantOrderTime>"+ (GlobalInfo.merchantOrderTime = Utils.getSendTime())+ "</merchantOrderTime>"
				+ "<merchantOrderAmt>"+ merchantOrderAmt+ "</merchantOrderAmt>"
				+ "<transType>" + "02" + "</transType>"
				+ "<gwType>" + "04" + "</gwType>"
				+ "<backUrl>" + "http://www.baidu.com" + "</backUrl>"
				+ "<merchantUserId>" + merchantUserId + "</merchantUserId>"
				+ "</BindPay>";
		return content;
	}
	
	public static String toValidCode(String mobile,String merchantId,String merchantOrderId){
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
				+ "<BindPay application=\"GetMobileMac.Req\" version=\"001-1.0.0\" sendTime=\""
				+ Utils.getSendTime()
				+ "\" sendSeqId=\"" + "1.0.0" + "\">"
				+ "<mobileNumber>" + mobile + "</mobileNumber>"
				+ "<merchantOrderId>" + merchantOrderId + "</merchantOrderId>"
				+ "<merchantId>" + merchantId + "</merchantId>"
				+ "<gwType>" + "04" + "</gwType>"
				+ "<bizType>" + "02" + "</bizType>"
				+ "</BindPay>";
		System.out.println("!!!!!!"+content);
		return content;
	}
	
	public static String toMGDirectPay(String checkType,String cardNum,
			String mobileMac,String idNum,String merchantId,String merchantOrderId,
			String merchantOrderAmt,String msgExt,String merchantUserId){
		String content = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
				+ "<BindPay application=\"MGDirectPay.Req\" version=\"001-1.0.0\" sendTime=\""
				+ Utils.getSendTime()
				+ "\" sendSeqId=\"" + "1.0.0" + "\">"
				+ "<merchantId>" + merchantId + "</merchantId>"
				+ "<merchantOrderId>"+ merchantOrderId+ "</merchantOrderId>"
				+ "<merchantOrderTime>"+ merchantOrderId + "</merchantOrderTime>"
				+ "<merchantOrderAmt>"+ merchantOrderAmt+ "</merchantOrderAmt>"
				+ "<merchantUserId>" + merchantUserId + "</merchantUserId>"
				+ "<transType>" + "01" + "</transType>"
				+ "<gwType>" + "04" + "</gwType>"
				+ "<backUrl>" + "http://www.baidu.com" + "</backUrl>"
				+ "<checkType>" + checkType + "</checkType>"
				+ "<cardNum>" + cardNum + "</cardNum>"
				+ "<msgExt>" + msgExt + "</msgExt>";
		
		if(mobileMac != null && "1".equals(checkType)){
			content += "<mobileMac>" + mobileMac + "</mobileMac>";
		}
				
		if(idNum != null && "2".equals(checkType)){
			idNum = idNum.substring(idNum.length()-4, idNum.length());
			content += "<idNum>" + idNum + "</idNum>";
			content += "<idType>" + "01" + "</idType>";			
		}
//		content += "<mobileNum>" + "15821989270" + "</mobileNum>";
		content += "</BindPay>";
		
		System.out.println("----------"+content);
		return content;
	}

}
